# -*- coding: cp1251 -*-

import urllib
import sys, urllib2
import cx_Oracle
#from lxml.html import parse
import lxml.html
from lxml import etree
# s https rabotaet normalno
# hotja google sna4ala dernul 4erez https a potom http
# http://bugs.python.org/issue1519816
# DataMime sites with hotels
class dmHotel:
    def __init__(self, site):
        self.site = site

    def get_site(self, url):
        url = 'http://goldenshara.com/viewforum.php?f=951'
        #url = 'http://tycho.usno.navy.mil/cgi-bin/timer.pl'
        usock = urllib2.urlopen(url)    
        _page =  usock.read()
       # print _page
        print '-------------------------------'
        # got lxml.etree
        ET = lxml.html.document_fromstring(_page)#.getroot()
        htmltree  = lxml.html.parse(url)
        root = htmltree.getroot()
      #  print(etree.tostring(root, pretty_print=True))
        print root[0]         
        for div in root.findall(".//table"): 
            print 'Start table'
            for a in div.findall(".//tr"):
                print a.tag, a.text
                for td in a.findall(".//td"):
                    print td.tag, td.text
            print 'End table'
      #  print ET.find("html/head")
      #  print ET.cssselect('.include_0')
#        for topic in ET.cssselect('.include_0'):
#           print topic       
 #       for topic in ET.findall('.//tr'):
  #          print topic.text
#            for t in topic:
              #print t

        conn = cx_Oracle.Connection("jony/jony@lreposit")
        cur = conn.cursor()
        cur.execute("""
                   begin
                      insert into code(file_name, line, full_test, schema_name) values(:var1,:var2,:var3, :var4);
                      commit;                  
                    end;  
                   """,var1='site',var2=1, var3 = _page, var4 = 'hz')
    def get_site_other(self):
        opener = urllib2.build_opener(
                 urllib2.HTTPHandler(),
                 urllib2.HTTPSHandler(),
                 #urllib2.ProxyHandler({'http': 'http://:@svr-122.amber.ua:3128'})
                 )
        urllib2.install_opener(opener)
        print urllib2.urlopen('http://www.booking.com').read()

#h = httplib2.Http(".cache")
#response, content = h.request('http://tycho.usno.navy.mil/cgi-bin/timer.pl')

#html_file = content.decode("utf-8")
#print(html_file)
if __name__ == '__main__':
    dm = dmHotel('test')
    dm.get_site('test')
